<?php


namespace App\Remote\Rms;

use App\Service\Shop\ShopService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

/**
 * Remote 类只做请求 不做业务处理
 */
class RmsShopRemote extends BaseRmsRemote
{

    //同步RMS
    const SHOP_CASH_REGISTER_STATUS_OK        = 0;
    const SHOP_CASH_REGISTER_STATUS_OFF       = 1;
    const SHOP_CASH_REGISTER_STATUS_CHECK_OFF = 2;
    const SHOP_CASH_REGISTER_STATUS_FEE_OFF   = 3;
    const SHOP_EXPIRED_OFF                    = 4;
    const SHOP_DEFER_EXPIRED_OFF              = 5;

    const SHOP_CASH_REGISTER_STATUS_MAPPING = [
        self::SHOP_CASH_REGISTER_STATUS_OK        => '正常',
        self::SHOP_CASH_REGISTER_STATUS_OFF       => '手动关闭',
        self::SHOP_CASH_REGISTER_STATUS_CHECK_OFF => '稽核关闭',
        self::SHOP_CASH_REGISTER_STATUS_FEE_OFF   => '年费关闭',
        self::SHOP_EXPIRED_OFF                    => '合同到期关闭',
        self::SHOP_DEFER_EXPIRED_OFF              => '续签延期到期关闭',
    ];


    /**
     * 更新门店信息
     *
     * @param int $shopCode
     * @param array $shopField
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function updateShopFieldByCode(int $shopCode, array $shopField)
    {
        return self::customRequest(
            "api/internal/shop/updateShopField",
            "post",
            [
                "conditions" => ['shopCode' => $shopCode],
                'shopField'  => $shopField
            ]
        );
    }

    /**
     * 通过shopId更新门店信息
     *
     * @param int $shopId
     * @param array $shopField
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function updateShopFieldById(int $shopId, array $shopField)
    {
        return self::customRequest(
            "api/internal/shop/updateShopField",
            "post",
            [
                "conditions" => ['id' => $shopId],
                'shopField'  => $shopField
            ]
        );
    }

    /**
     * 获取门店信息 默认 60s 缓存
     *
     * @param array $shopCode
     * @param array|string[] $field
     * @return mixed
     */
    public static function getShopFieldsByCodes(array $shopCode, array $field = [])
    {
        $shopCodeStr = implode(',', $shopCode);
        $fieldStr    = implode(',', $field);
        return Cache::remember('rmsRemote:getShopNameByCodes:' . md5($shopCodeStr), getSwitchCacheTime(60),
            function () use ($shopCodeStr, $fieldStr) {
                return self::customRequest(
                    "api/internal/shop/getShopByCodes",
                    "get",
                    [
                        "shopCodes" => $shopCodeStr,
                        'brandId'   => 1,
                        'fields'    => $fieldStr
                    ]
                );
            });
    }

    public static function getShopInfosByCodes(array $shopCode)
    {
        return Http::withOptions([
            'http_errors' => false,
        ])->withHeaders([
            'internal-key'   => config('rms.internalKey'),
            'internal-token' => config('rms.internalValue'),
            'accept'         => 'application/json',
        ])->get(config('rms.domain') . 'api/internal/shop/getShopByCodes', [
            'shopCodes' => implode(',', $shopCode),
            'brandId'   => 1,
            'fields'    => '',
        ])->json('data');
    }

    /**
     * 获取开业信息数据
     *
     * @param int $shopCode
     * @param string $mobile
     * @param int $formalUseAt
     * @return mixed
     */
    public static function getOpenShopStartBusiness(int $shopCode, string $mobile, int $formalUseAt = 0)
    {
        return Cache::remember('rmsRemote:getOpenShopStartBusiness:' . $shopCode, getSwitchCacheTime(60),
            function () use ($shopCode, $mobile, $formalUseAt) {
                return self::customRequest(
                    "api/internal/shop/getOpenShopInfo",
                    "get",
                    [
                        "shopCode"    => $shopCode,
                        'mobile'      => $mobile,
                        'formalUseAt' => $formalUseAt
                    ]
                );
            });
    }


    /**
     * 获取门店功能开通信息
     *
     * @param int $shopId
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function functionOpenDetail(int $shopId)
    {
        return self::customRequest(
            "api/internal/shop/functionOpenDetail",
            "get",
            [
                "shopId" => $shopId
            ]
        );
    }


    /**
     * 选址报告提交 - 同步门店地址信息
     *
     * @param int $shopId
     * @param string $address
     * @param string $addressPrefix
     * @param string $province
     * @param string $city
     * @param string $district
     * @param string $lng
     * @param string $lat
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function synchronizeShopInfo(
        int $shopId,
        string $address,
        string $addressPrefix,
        string $province,
        string $city,
        string $district,
        string $lng,
        string $lat
    ) {
        return self::customCacheRequest(
            "api/internal/shop/synchronizeShopInfo",
            RequestAlias::METHOD_POST,
            [
                "id"            => $shopId,
                "address"       => $address,
                "addressPrefix" => $addressPrefix,
                "province"      => (string)$province,
                "city"          => (string)$city,
                "district"      => (string)$district,
                "lng"           => (string)$lng,
                "lat"           => (string)$lat,
            ]
        );
    }

    /**
     * 凑备确认施工完成，门店启用
     *
     * @param int $shopId
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function switchShopOn(int $shopId)
    {
        return self::customRequest(
            "api/internal/shop/switchShopOn",
            RequestAlias::METHOD_POST,
            [
                "shopId" => $shopId
            ]
        );
    }

    /**
     * 闭店
     *
     * @param int $shopCode
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function closeShop(int $shopCode)
    {
        return self::customRequest(
            "api/internal/shop/closeShop",
            RequestAlias::METHOD_POST,
            [
                "shopCode" => $shopCode
            ]
        );
    }

    /**
     * 修改门店手机号
     *
     * @param int $shopCode
     * @param string $mobile
     * @param string $contact
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function updateShopMobile(int $shopCode, string $mobile, string $contact)
    {
        return self::customRequest(
            "api/internal/shop/updateShopMobile",
            RequestAlias::METHOD_POST,
            [
                "shopCode" => $shopCode,
                "mobile"   => $mobile,
                "contact"  => $contact
            ]
        );
    }

    /**
     * 城市门店
     *
     * @param int $cityCode
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function cityShop(int $cityCode, string $shopName = '')
    {
        return self::customCacheRequest(
            "api/internal/shop/cityShop",
            RequestAlias::METHOD_GET,
            [
                "cityCode" => $cityCode,
                'name'     => $shopName
            ],
            null,
            30 * 60
        );
    }

    /**
     * @param string $searchWhere
     * @param bool $withFields
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getShopInfoByLikeSearch(string $searchWhere, bool $withFields = false)
    {
        return self::customCacheRequest(
            "api/internal/shop/getShopInfoByLikeSearch",
            RequestAlias::METHOD_GET,
            [
                "searchWhere" => $searchWhere,
                "withFields"  => (int)$withFields
            ],
            null,
            5 * 60
        );
    }

    /**
     * @param array $shopIds
     * @param array $fields
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getShopInfoByIds(array $shopIds, array $fields = [])
    {
        return self::customCacheRequest(
            "api/internal/shop/getShopInfos",
            RequestAlias::METHOD_GET,
            [
                'shopIds' => implode(',', $shopIds),
                'fields'  => implode(',', $fields),
                'brandId' => 1,
            ],
            null,
            5 * 60
        );
    }

    /**
     * 获取所有在营业店铺
     * @param string $date
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getFormalUseShopLists(string $date)
    {
        return self::customCacheRequest(
            "api/internal/shop/getFormalUseShopLists",
            RequestAlias::METHOD_GET,
            ['date' => $date],
            null,
            5 * 60
        );
    }


    /**
     * 获取门店详细信息
     *
     * @param int $shopId
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getShopDetailByShopId(int $shopId)
    {
        return self::customCacheRequest(
            "api/internal/shop/getShopDetailByShopId",
            RequestAlias::METHOD_GET,
            ["shopId" => $shopId],
            null,
            5 * 60
        );
    }

    /**
     * 获取门店列表
     *
     * @param int $onlyCount
     * @param array $provinceCodes
     * @param array $cityCodes
     * @param array $areaCodes
     * @return array|void
     * @throws \Exception
     */
    public static function getShopListByArea(
        int $onlyCount = 1,
        array $provinceCodes = [],
        array $cityCodes = [],
        array $areaCodes = []
    ) {
        return ShopService::getShopListByArea((object)[
                "provinceCodes" => $provinceCodes,
                "cityCodes"     => $cityCodes,
                "areaCodes"     => $areaCodes,
                "onlyCount"     => $onlyCount
        ]);
//        return self::customCacheRequest(
//            "api/internal/shop/kms/getShopListByArea",
//            RequestAlias::METHOD_POST,
//            [
//                "provinceCodes" => $provinceCodes,
//                "cityCodes"     => $cityCodes,
//                "areaCodes"     => $areaCodes,
//                "onlyCount"     => $onlyCount
//            ],
//            null
//        );
    }


    /**
     * 获取门店风云榜
     * @param array $requestArray
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getWindCloudRank(array $requestArray)
    {
        return self::customCacheRequest(
            "api/internal/shop/windCloud",
            RequestAlias::METHOD_GET,
            $requestArray,
            null,
            5 * 60
        );
    }


    /**
     * 获取营业指标数据
     * @param array $requestArray
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getDashboardCruise(array $requestArray)
    {
        return self::customCacheRequest(
            "api/internal/shop/dashboard/cruise",
            RequestAlias::METHOD_GET,
            $requestArray,
            null,
            5 * 60
        );
    }


    /**
     * 门店富有支付配置新增
     * @param int $shopId
     * @param int $paymentId
     * @param array $requestArray
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function shopFuYouPaymentsEdit(int $shopId, int $paymentId, array $requestArray)
    {
        return self::customCacheRequest(
            "api/internal/shop/shopFuYouPaymentsEdit/" . $shopId . '/' . $paymentId,
            RequestAlias::METHOD_POST,
            $requestArray,
        );
    }



    /**
     * 查询门店属性
     * @param array $shopIds
     * @throws GuzzleException
     */
    public static function getShopAttribute(array $shopIds)
    {
        $params = [
            'shopIds' => $shopIds,
        ];

        return self::customCacheRequest(
            'api/internal/shop/getShopAttribute',
            RequestAlias::METHOD_GET,
            $params,
        );
    }


    /**
     * 编辑门店属性
     * @param int $shopId
     * @param array $data
     * @throws GuzzleException
     */
    public static function updateShopAttribute(int $shopId, array $data)
    {
        return self::customCacheRequest(
            'api/internal/shop/updateShopAttribute/' . $shopId,
            RequestAlias::METHOD_POST,
            $data,
        );
    }


    /**
     * 查询门店属性配置项
     * @throws GuzzleException
     */
    public static function getShopAttributeData()
    {
        return self::customCacheRequest(
            'api/internal/shop/getShopAttributeData',
            RequestAlias::METHOD_GET,
            null,
            null,
            3 * 60
        );
    }


    /**
     * 同步观远门店信息
     * @throws GuzzleException
     */
    public static function syncGuanYuanShopList($list)
    {
        return self::customCacheRequest(
            'api/internal/shop/syncGuanYuanShopList',
            RequestAlias::METHOD_POST,
            ['list' => $list],
            ['content-type' => 'application/json'],
        );
    }
}
